package edu.xaut.bookingsys.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xaut.bookingsys.entity.CourseDuty;
import edu.xaut.bookingsys.mapper.CourseDutyMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class CourseDutyService extends ServiceImpl<CourseDutyMapper, CourseDuty> {

    public List<CourseDuty> getWeekDutyByDate(LocalDate date) {
        LocalDate from = date.minusDays(date.getDayOfWeek().getValue()-1);
        LocalDate to = date.plusDays(7 - date.getDayOfWeek().getValue());
        return getDutyByDate(from, to);
    }

    public List<CourseDuty> getDutyByDate(LocalDate from, LocalDate to) {
        LambdaQueryWrapper<CourseDuty> wrapper = new LambdaQueryWrapper<>();
        return getBaseMapper().selectList(
                wrapper.ge(CourseDuty::getCurDate, from)
                        .and(item -> item.le(CourseDuty::getCurDate, to)));
    }
}
